﻿Imports System.Text.RegularExpressions

Public Class HiddenForm2
    Dim countresults As Integer
    'Contains Bing Data

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim resultslist As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("li")
        Dim resultitem As HtmlElement
        Dim urla As String
        Dim name As String
        Dim temp As String
        Dim desc As String = MainSearchForm.txtSearchString.Text & ""
        Dim desc1 As String = MainSearchForm.txtSearchString.Text
        countresults = 0

        For Each resultitem In resultslist
            If countresults < 10 Then
                If resultitem.GetAttribute("classname") = "sa_wr" Then
                    urla = resultitem.FirstChild.FirstChild.FirstChild.FirstChild.GetAttribute("href").ToString()
                    name = resultitem.FirstChild.FirstChild.FirstChild.FirstChild.InnerHtml
                    desc1 = resultitem.FirstChild.InnerHtml
                    temp = Replace(name, "<strong>", "")
                    name = Replace(temp, "</strong>", "")

                    Dim RegexObj As New Regex("<p>(.*)</p>", RegexOptions.IgnoreCase)
                    Dim MatchResults As Match = RegexObj.Match(desc1)
                    While MatchResults.Success
                        desc = MatchResults.Groups(1).Value
                        temp = Replace(desc, "<strong>", "")
                        temp = Replace(temp, "<STRONG>", "")
                        temp = Replace(temp, "nbsp;", "")
                        temp = Replace(temp, "</STRONG>", "")
                        desc = Replace(temp, "</strong>", "")
                        MatchResults = MatchResults.NextMatch()
                    End While


                    MainSearchForm.apotelesmata.addResult(name, urla, "bing", desc)
                    countresults = countresults + 1
                    Try
                        MainSearchForm.ProgressBar1.Value = MainSearchForm.ProgressBar1.Value + 1
                    Catch
                    End Try
                End If
            Else
                Exit For
            End If
        Next

        resultslist = WebBrowser1.Document.GetElementsByTagName("li")
        For Each resultitem In resultslist
            If MainSearchForm.apotelesmata.bingcount < 99 Then
                If Not (resultitem.FirstChild Is Nothing) Then
                    If resultitem.FirstChild.GetAttribute("classname") = "sb_pagN" Then
                        urla = resultitem.FirstChild.GetAttribute("href").ToString()
                        WebBrowser1.Navigate(urla)
                        'Exit Sub
                    End If
                End If
            Else
                MainSearchForm.flag2 = True
                MainSearchForm.ReadyToView()
                Exit For
            End If
        Next
    End Sub
End Class